import read_all
import histo_test
import show_gabor
import LSD
import remove_sigma
import perspective_transform
import GLCM

import numpy as np
import cv2
import matplotlib.font_manager as fm
import os
import matplotlib.pyplot as plt

import detector

def cv_show(title,img,block=False):
    return histo_test.cv_show(title,img,block)

def cv_read(img_file,gray=True):
    return histo_test.cv_read(img_file,gray)

def AdaptiveEquHisto(img):
    return histo_test.AdaptiveEquHisto(img)

def gen_path(origin, suffix, folder_suffix=None):
    return read_all.gen_path(origin, suffix, folder_suffix)

def gen_new_path(origin, new_class, new_img_name):
    return read_all.gen_new_path(origin, new_class, new_img_name)

def save_img(dest_path, img):
    return read_all.save_img(dest_path, img)

def display_imgs(imgs, titles, num_cols=5, main_title=None):
    return histo_test.display_imgs(imgs, titles, num_cols, main_title)

def display_hists(datas, titles=None, num_cols=5, main_title=None):
    return histo_test.display_hists(datas, titles, num_cols, main_title)

def gen_params_set():
    return show_gabor.gen_params_set()

def gen_params_set_theta_range(begin, end, step):
    return show_gabor.gen_params_set_theta_range(begin, end, step)

def build_filters(params_set):
    return show_gabor.build_filters(params_set)

def gen_params_string(params):
    return show_gabor.gen_params_string(params)

def get_font_times(fontsize):
    return fm.FontProperties(
        fname='/usr/share/fonts/truetype/msttcorefonts/times.ttf', size=fontsize)

def get_font_yahei(fontsize):
    return fm.FontProperties(
        fname='/home/hugoxana/.local/share/fonts/msyh.ttf', size=fontsize)

def get_roi(img, top_left, buttom_right):
    return img[top_left[1]:buttom_right[1], top_left[0]:buttom_right[0]] # 图像切片的第一个参数是高度，第二个参数是宽度,并且图像的最左上角坐标是（0，0）

def grid_slice_row_col(img, row, col):
    slices = []
    [height, width] = img.shape
    slice_width = width // col
    slice_height = height // row
    for i in range(row):
        for j in range(col):
            slice_up = i * slice_height
            slice_down = slice_up + slice_height
            slice_left = j * slice_width
            slice_right = slice_left + slice_width
            slices.append(get_roi(img,[slice_left, slice_up],[slice_right, slice_down]))
    return slices

def grid_slice_width_height(img, width, height):
    slices = []
    [img_height, img_width] = img.shape
    row = img_height // height
    col = img_width // width
    for i in range(row):
        for j in range(col):
            slice_up = i * height
            slice_down = slice_up + height
            slice_left = j * width
            slice_right = slice_left + width
            slices.append(get_roi(img,[slice_left, slice_up],[slice_right, slice_down]))
    return slices

def grid_slice_width_height_grid(img, width, height):
    slices = []
    [img_height, img_width] = img.shape
    row = img_height // height
    col = img_width // width
    for i in range(row):
        for j in range(col):
            slice_up = i * height
            slice_down = slice_up + height
            slice_left = j * width
            slice_right = slice_left + width
            slices.append(get_roi(img,[slice_left, slice_up],[slice_right, slice_down]))
    return slices, (row,col)

def draw_lines(img, lines, color, width=2):
    return LSD.draw_lines(img, lines, color, width)

def draw_points(img, points, color, radius, line_width):
    return LSD.draw_points(img, points, color, radius, line_width)

def get_slope(line):
    return LSD.get_slope(line)

def calc_mean_slope(roi):
    return LSD.calc_mean_slope(roi)

def remove_outliers_sigma(array, factor=3):
    return remove_sigma.remove_outliers_sigma(array, factor)

def outliers_sigma_indices(array, factor):
    return remove_sigma.outliers_sigma_indices(array, factor)

def PerspectiveTransform(img, src, dst, result_size):
    return perspective_transform.PerspectiveTransform(img, src, dst, result_size)

def calc_glcm_coprops(img, graylevel,dist,degree,show=False,main_title=None):
    return GLCM.calc_glcm_coprops(img, graylevel, dist, degree, show,main_title)


